package com.example.redisscene.service;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 需求：开门过程中后台一直去获取货柜状态，将这些获取到的状态返回到redis中缓存起来，然后由前台过来获取
 */
@Service
public class ListService {
    private static final String KEY = "PROCESS";

    @Resource
    private RedisTemplate<String,String> redisTemplate;


    public void setOrderInfo(String orderNumber, String info) {
        Long orders = redisTemplate.opsForList().rightPush(KEY+":"+orderNumber, info);
        System.out.println(orders);
    }

    public void getOrderInfo(String orderNumber) {
        Optional<String> firstOne = Optional.ofNullable(redisTemplate.opsForList().index(KEY+":"+orderNumber, 0));
        Optional<String> lastOne = Optional.ofNullable(redisTemplate.opsForList().index(KEY+":"+orderNumber, -1));
        System.out.println(firstOne);
        System.out.println(lastOne);
    }

    public void deleteByKey(String orderNumber) {
        redisTemplate.delete(KEY+":"+orderNumber);
    }
}
